#include <Ogre.h>
#include <boost/foreach.hpp>
#include <time.h>
#include <string>

#include "wonderland.h"
#include "xmpp_connection.h"
#include "serialize.h"
#include "headless.h"
#include "deserialize.h"

Wonderland::Headless *region;
Wonderland::XmppConnection *xmpp;

void onmessage(std::string from, std::string message){
  std::cout << "message recieved from" << from << std::endl;
  Wonderland::Deserialize(message, region);
}

void onerror(std::string err){
  std::cout << "[ERROR]" << err << std::endl;
  exit(-1);
}

int main(int argc, char **argv){
  using namespace Wonderland;
  
  xmpp = new XmppConnection;
  xmpp->setNickname("server");
  xmpp->setServer("wonderlandproject.info");
  xmpp->setPassword(Wonderland::Utility::slurp("/Users/ben/.wonderland/server_password"));
  xmpp->connect();

  xmpp->signal_error.connect(&onerror);
  xmpp->signal_message.connect(&onmessage);

  region = new Headless;
  region->initialize();
  region->start();
  
  int cont = 1,
    i = 0;
  
  while(cont){
    xmpp->poll();

    if(i%100==0){
      BOOST_FOREACH(std::string jid, xmpp->subscribers_){
//        xmpp->send(jid, Serialize(region->getSceneManager()->getRootSceneNode()));
      }

//      std::cout << Serialize(region->getSceneManager()->getRootSceneNode()) << std::endl << std::endl;
    }

    usleep(10000);
    
    i++;
  }

  return 0;
}